"""Tests that TAPA CLI can reuse the work directory."""

# Copyright (c) 2025 RapidStream Design Automation, Inc. and contributors.
# All rights reserved. The contributor(s) of this file has/have agreed to the
# RapidStream Contributor License Agreement.

load("@bazel_skylib//rules:diff_test.bzl", "diff_test")

genrule(
    name = "vadd-xo",
    srcs = [
        "//tests/apps/vadd:vadd.cpp",
        "//tests/apps/vadd:vadd.h",
    ],
    outs = ["vadd.xo"],
    cmd_bash = """
set -ex
work_dir="work.out"
platform="xilinx_u250_gen3x16_xdma_4_1_202210_1"

$(location //bazel:vitis_hls_env) $(location //tapa) --work-dir "$${work_dir}" \
    analyze -c -I$$(dirname $(location //tests/apps/vadd:vadd.h)) \
    --input $(location //tests/apps/vadd:vadd.cpp) --top VecAdd \

$(location //bazel:vitis_hls_env) $(location //tapa) --work-dir "$${work_dir}" \
    synth --platform "$${platform}" \

$(location //bazel:vitis_hls_env) $(location //tapa) --work-dir "$${work_dir}" \
    synth --platform "$${platform}" --skip-hls-based-on-mtime \
    link \

$(location //bazel:vitis_hls_env) $(location //tapa) --work-dir "$${work_dir}" \
    pack --output $@ \

""",
    tools = [
        "//bazel:vitis_hls_env",
        "//tapa",
    ],
)

# Using `diff_test` since TAPA generated XO are (supposed to be) reproducible.
diff_test(
    name = "reuse-work-dir-test",
    file1 = ":vadd-xo",
    file2 = "//tests/apps/vadd:vadd-xo",
)
